//
//  FeedBackAddPhotosVM.swift
//  CooperSwift_Example
//
//  Created by 计海峰 on 2022/1/12.
//  Copyright © 2022 CocoaPods. All rights reserved.
//

import Foundation
import UIKit
import CooperSwift

class FeedBackAddPhotosVM : TableShowCollectionBaseVM {
    
    override func initDown() {
        super.initDown()
        
//        MARK: - 默认修改配置 演示  不要删  不要删  不要删
        
        guard let cvc = cvcByKey(default_cell_map_key) else { return }
//        cvc.1.bgColor = UIColor.random()
        
//        cvc.1.bindBlock = { [weak self] cv, cell, config in
//            cell.backgroundColor = .random()
//        }
        
//        cvc.1.configBlock = { [weak self] cv, cell, config in
//            guard let layout = cv.collectionViewLayout as? UICollectionViewFlowLayout else { return }
//            layout.minimumLineSpacing = 8
//            layout.minimumInteritemSpacing = 8
////            cv.snp.remakeConstraints { make in
//                make.edges.equalToSuperview().inset(UIEdgeInsets(top: 8, left: 8, bottom: 8, right: 8))
//            }
//        }
        
//        cvc.0.moveItem = { [weak self] dataSource, sourceIndexPath, destinationIndexPath in
//
//            guard let cvc = self?.cvcByKey(self?.default_cell_map_key) else { return }
//
//            guard var section = cvc.0.sections[safe: sourceIndexPath.section] else { return }
//            guard let item = section.items[safe: sourceIndexPath.row] else { return }
//            section.items.remove(at: sourceIndexPath.row)
//            cvc.0.sections[sourceIndexPath.section] = section
//
//            guard var toSection = cvc.0.sections[safe: destinationIndexPath.section] else { return }
//            toSection.items.insert(item, at: destinationIndexPath.row)
//            cvc.0.sections[destinationIndexPath.section] = toSection
//
//        }
        
        var items = Array<CollectionCellConfigProtocol>()
        
        guard let cvcVM = cvcByKey(default_cell_map_key)?.0 else { return }
        
        for i in 0...4 {
            
            let urlBTModel = EditImageControlModel().then {
                $0.url = "https://dss1.bdstatic.com/kvoZeXSm1A5BphGlnYG/skin_zoom/\(i+320).jpg"
            }.addTargetKey(cvcVM.bindKey(bag), isTarget: true)
            let urlModel = CollectionEditImageCVCModel.init(urlBTModel).then {
    //            $0.cellSize = CGSize((Screen.width - 32)/3.0 - 0.1, (Screen.width - 32)/3.0 - 0.1)
                $0.isLongPress = true
            }.addTargetKey(cvcVM.bindKey(bag), isTarget: true)
            items.append(urlModel)
            
        }
        let addBTModel = EditImageControlModel().then {
            $0.imageName = "icon_item_image_add"
            $0.isShow = false
        }.addTargetKey(cvcVM.bindKey(bag), isTarget: true)
        let model = CollectionEditImageCVCModel.init(addBTModel).then {
//            $0.cellSize = CGSize((Screen.width - 32)/3.0 - 0.1, (Screen.width - 32)/3.0 - 0.1)
            $0.isLocationLast = true
            $0.isLongPress = false
        }.addTargetKey(cvcVM.bindKey(bag), isTarget: true)
        model.isLocationLast = true
        
        items.append(model)
        
        cvcVM.addFirstSectionItems(items)
        
        let btnConfig = TableShowBtnConfig("提交").then {
            $0.method = "sumbit"
        }
        let btnCell = TableShowBtnCellModel(btnConfig).then {
            $0.cellHeight = 44
        }.addTargetKey(bindKey(bag), isTarget: true)
        
        addFirstSectionItems([lineRow(12), btnCell])
        
    }
    
    // MARK: - UITableViewDelegate
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let headerV = TestAutoHeaderV.loadFromNib()
        headerV.titleLB.text = "测试"
        return headerV
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 44.0
    }
    
    @objc func sumbit(_ btn: Any) {
        
    }
    
    override func calculateHeight(_ key: String? = nil) {
        let map_key = key ?? default_cell_map_key
        guard let cvcVM = cvcByKey(map_key)?.0 else { return }
        if cvcVM.firstSection().items.count == 0 {
            refreshHeight(map_key, 0.0)
            return
        }
        let cellHeight = ((Screen.width - 32)/3.0 - 0.01 + 8) * CGFloat(((cvcVM.firstSection().items.count - 1) / 3 + 1)) + 8
        refreshHeight(map_key, cellHeight)
    }
    
}
